package com.kcs.fxmargin.service;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.PropsUtils;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.dao.CommonDao;
import com.kcs.fxmargin.model.CConfigSys;

public class FilesService {
	private static Logger log = Logger.getLogger(Constant.LOG_FX);
	private static String AUTO_DEL_FILE_MONTH 	= "AUTO_DEL_FILE_MONTH";
	private static String FILE_PATH_UNIX		= "FILE_PATH_UNIX";
	
	private static CommonDao dao = new CommonDao();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			deleteFile();
		}catch (Exception e){
			e.printStackTrace();
		}
		
		
	}
	
	public static void deleteFile() throws SQLException {
		Connection conn 				= null;
		PreparedStatement pstm 			= null;
		ResultSet rs					= null;
		try{
			ResourceBundle prop = PropsUtils.getProps(Constant.PROP_CONFIG_SYSTEM);
		
			String realPath = prop.getObject("file_path_unix").toString();
			String autoDel	= prop.getObject("auto_del_file_month").toString();
			
//			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
//			List<CConfigSys> configList = dao.getConfig(conn, pstm, rs);
//			
//			for(CConfigSys config : configList){
//				if(FILE_PATH_UNIX.equals(config.getKeys())) realPath = config.getValue();
//				else if(AUTO_DEL_FILE_MONTH.equals(config.getKeys())) autoDel = config.getValue();
//			}
			
			Integer nowDate = Integer.parseInt(commonUtils.formatDate(new Date(), "yyyyMM"));
			
			log.info("nowDate = " + nowDate);
			
			log.info("Called deleteFiles");
//	        File file 	= new File("D:/test/");
	        File file 	= new File(realPath + "/");
	        File myFile = null;
	        Integer fileNo = 0;
	        Integer index = nowDate - Integer.parseInt(autoDel);
	        
	        log.info("Real Path = " + realPath);
	        log.info("Real Index = " + index);

	        String[] myFiles;
	        if (file.isDirectory()) {
	            myFiles = file.list();
	            for (int i = 0; i < myFiles.length; i++) {
	                myFile = new File(file, myFiles[i]);
	                log.info(myFile);
		            
	                fileNo = Integer.parseInt(myFile.getName());
	                
//	                log.debug("fileNo = " + fileNo);
	                
	                if(fileNo <= index){
	                	log.info("delete folder = " + myFile.toString());
	                	checkClientFile(realPath + "/" + myFile.getName());
	                	myFile.delete();
	                }
	            }
	        }
			
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
	}
	
	
	public static void checkClientFile(String directory) throws Exception {
		File file 	= new File(directory);
		File myFile = null;
		
		String[] myFiles;
		if(file.isDirectory()){
			myFiles = file.list();
			for (int i = 0; i < myFiles.length; i++) {
				myFile = new File(file, myFiles[i]);
				log.info("delete folder = " + myFile.toString());
				
				checkClientFile(directory + "/" + myFile.getName());
				
				myFile.delete();
			}
		}
	}
	
	public static void deleteLogFile()throws Exception{
		ResourceBundle prop = PropsUtils.getProps(Constant.PROP_CONFIG_SYSTEM);
		String logPath = prop.getObject("log_path_unix").toString();
		File file = new File(logPath);
		long l = 0;
		Date date = null;
		Integer nowDate = Integer.parseInt(commonUtils.formatDate(new Date(), "yyyyMMdd"));
		Integer fileDate = null;
		int autoDel = Integer.parseInt(prop.getObject("auto_del_file_month").toString());
		
		System.out.println("**Delete log files currentDate-"+autoDel);
		System.out.println("logPath: "+logPath);
		
		for(File f : file.listFiles()){
			if(f.isFile()){
				l = f.lastModified();
				date = new Date(l);
				fileDate = Integer.parseInt(commonUtils.formatDate(date, "yyyyMMdd"));
				
				if(fileDate<nowDate-autoDel){
					f.delete();
					System.out.println("Delete: "+f.getName());
				}
			}
		}

		System.out.println("Delete log file complete***");

	}

}
